package utils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import dbhelper.Items;
import dbhelper.Users;
import entities.Item;
import entities.UpdatedItem;
import entities.User;
public class ItemsHelper {
private static final String DFAULT_IMAGE_URL = 
"https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcTV2AUaBUiAY2NbfTq2ZYxx4vxf1dgcF4wp-IKonZ980YK-CxUJyw";
private static final double DEFAULT_BIT_AMOUNT = 0.1;
private static final java.sql.Timestamp DEFAULT_SELL_TIMEOUT = new java.sql.Timestamp(1000 * 60 * 10 ); 
// 10 min

/**
 * sets the values to the default if needed and adds it to the db 
 * @param item
 * @return true if successful 
 */
public static boolean postItem(Item item){
		if(item.getName() != null && !item.getName().isEmpty()
				&& item.getDescription() != null && !item.getDescription().isEmpty()){
			item.setPictureURL(
					StringUtils.isNotBlank(item.getPictureURL()) ? item.getPictureURL() : DFAULT_IMAGE_URL
							); 
			item.setCurrentBidAmount(
					item.getCurrentBidAmount() != 0 ?  item.getCurrentBidAmount() : DEFAULT_BIT_AMOUNT
					);
			item.setPublishTime(
					new java.sql.Timestamp(Calendar.getInstance().getTimeInMillis())
					);
			item.setSellTime(
					item.getSellTime().getTime() != 0 ? item.getSellTime(): DEFAULT_SELL_TIMEOUT 
					);
			item.setSellTime(
					new Timestamp(item.getSellTime().getTime() + item.getPublishTime().getTime()) 
					);
			return Items.postNewItem(item);
		}
			return false;
				
	}


public static List<Item> getNewItems(int itemsNumber,Timestamp startFrom){
return Items.getNewItems(itemsNumber,startFrom);	
}

public static long getNumberOfSoldItems(){
	return Items.getNumberOfSoldItems();
}

public static long getNumberOfItemsForSale(){
	return Items.getNumberOfItemsForSale();
}

public static List<Item> search(String searchText, Timestamp startFrom, int pageNumber){
	searchText = searchText.replaceAll("/%',-\\\\", "");
	String words[] = searchText.split(" ");
	if(pageNumber < 1)
		pageNumber = 0;
	return (words.length > 0) ?
			Items.searchItems(
			Arrays.asList(words), startFrom,pageNumber
			) : new ArrayList<Item>();
}

/**
 * sets for each item if it belongs to the user or not
 * @param items
 * @param userID
 */
public static void isUsersItem(List<Item> items,long userID){
	if(items != null)
	for(Item item : items){
		item.setMine(item.getSellerID() == userID);
	}
}

/**
 * 
 * @param itemsToUpdate
 * @return the new data to be updated (winner userID if exists , currentBidAmount)
 */
public static List<UpdatedItem> getUpdatedItems(List<Long> itemsToUpdate){
	List<UpdatedItem> result = new ArrayList<UpdatedItem>();
	if(itemsToUpdate != null)
	for(int i = 0;i < itemsToUpdate.size(); i++){
		long itemID = itemsToUpdate.get(i);
		if(itemID < 0) break;
		User user = Users.getUserByID(
				Items.getWinnerID(itemID));
		String winnerUserName = (user != null)	? user.getUserName() : null;	
		result.add(
				new UpdatedItem(
							winnerUserName
							,Items.getCurrentBid(itemID)
						)
				);
	}
	return result;
}

}